#include "historymessagewidget.h"
#include "debug.h"

////////////////////////////////////////////////////////////////////
/// 表示一个历史消息元素
////////////////////////////////////////////////////////////////////
HistoryItem *HistoryItem::makeHistoryItem(const model::Message &message)
{
    // 1. 创建对象
    HistoryItem *item = new HistoryItem();
    item->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    // 2. 创建布局
    QGridLayout *layout = new QGridLayout();
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(10);
    layout->setContentsMargins(0, 0, 0, 0);
    item->setLayout(layout);

    // 3. 创建头像
    QPushButton *avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40, 40);
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    avatarBtn->setIconSize(QSize(40, 40));
    // 设置当前发送者的头像
    avatarBtn->setIcon(message.sender.avatar);
    avatarBtn->setStyleSheet("QPushButton {border: none; }");

    // 4. 创建昵称时间
    QLabel *nameLabel = new QLabel();
    nameLabel->setText(message.sender.nickname + "|" + message.time);
    nameLabel->setFixedHeight(20); // 高度设置为头像高度的一半
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 5. 消息内容部分
    QWidget *contentWidget = nullptr;
    if (message.messageType == model::TEXT_TYPE)
    {
        // 文本消息
        QLabel *label = new QLabel();
        label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        label->setWordWrap(true);
        label->setText(QString(message.content));
        label->adjustSize(); // 让 label 自动调整大小
        contentWidget = label;
    }
    else
    {
        LOG() << "错误的消息类型！mesageType=" << message.messageType;
    }

    // 6. 把控件加入布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(contentWidget, 1, 1, 5, 1);

    return item;
}

HistoryMessageWidget::HistoryMessageWidget(QWidget *parent)
{
    // 1. 设置窗口本身属性
    this->setFixedSize(600, 600);
    this->setWindowTitle("历史消息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(30, 30, 30, 30);
    this->setLayout(layout);

    // 3. 创建单选按钮
    _keyRadioBtn  = new QRadioButton();
    _timeRadioBtn = new QRadioButton();
    _keyRadioBtn->setText("按关键词查询");
    _timeRadioBtn->setText("按时间查询");
    // 默认按关键词查询
    _keyRadioBtn->setChecked(true);
    layout->addWidget(_keyRadioBtn, 0, 0, 1, 2);
    layout->addWidget(_timeRadioBtn, 0, 2, 1, 2);

    // 4. 创建搜索框
    _searchEdit = new QLineEdit();
    _searchEdit->setFixedHeight(50);
    _searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _searchEdit->setPlaceholderText("输入关键词");
    _searchEdit->setStyleSheet("QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); "
                               "font-size: 16px; padding-left: 10px; }");
    layout->addWidget(_searchEdit, 1, 0, 1, 8);

    // 5. 创建搜索按钮
    QPushButton *searchBtn = new QPushButton();
    searchBtn->setFixedSize(50, 50);
    searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    searchBtn->setIconSize(QSize(30, 30));
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(220, 220, 220); }";
    searchBtn->setStyleSheet(btnStyle);
    layout->addWidget(searchBtn, 1, 8, 1, 1);

    // 6. 创建时间相关的部分控件，初始状态隐藏
    QLabel *begTag = new QLabel();
    begTag->setText("开始时间");
    QLabel *endTag = new QLabel();
    endTag->setText("结束时间");
    _begTimeEdit = new QDateTimeEdit();
    _endTimeEdit = new QDateTimeEdit();
    _begTimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm");
    _endTimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm");
    _begTimeEdit->setFixedHeight(40);
    _endTimeEdit->setFixedHeight(40);
    begTag->hide();
    endTag->hide();
    _begTimeEdit->hide();
    _endTimeEdit->hide();

    // 7. 创建滚动区域
    initScrollArea(layout);

    // 8. 设置槽函数
    connect(_keyRadioBtn, &QRadioButton::clicked, this,
            [=]()
            {
                // 隐藏时间相关的控件
                layout->removeWidget(begTag);
                layout->removeWidget(endTag);
                layout->removeWidget(_begTimeEdit);
                layout->removeWidget(_endTimeEdit);
                begTag->hide();
                endTag->hide();
                _begTimeEdit->hide();
                _endTimeEdit->hide();

                // 把关键词搜索显示加入布局
                layout->addWidget(_searchEdit, 1, 0, 1, 8);
                _searchEdit->show();
            });
    connect(_timeRadioBtn, &QRadioButton::clicked, this,
            [=]()
            {
                // 隐藏关键词相关的控件
                layout->removeWidget(_searchEdit);
                _searchEdit->hide();

                // 把关键词搜索显示加入布局
                layout->addWidget(begTag, 1, 0, 1, 1);
                layout->addWidget(endTag, 1, 4, 1, 1);
                layout->addWidget(_begTimeEdit, 1, 1, 1, 3);
                layout->addWidget(_endTimeEdit, 1, 5, 1, 3);
                begTag->show();
                endTag->show();
                _begTimeEdit->show();
                _endTimeEdit->show();
            });

    // 构造测试数据
#if TEST_UI
    for (int i = 0; i < 30; ++i)
    {
        // 注意此处代码和前面的差别.
        // 前面有个代码, UserInfo 必须要 new 出来才能构造. 当时 Item 对象里, 持有了 const UserInfo& , 不是 new 的话
        // 就可能使引用指向的对象失效的.
        // 此处后续的代码, 都是按照传值的方式来使用 message 的内容, 不 new 也行.
        model::UserInfo sender;
        sender.userId          = "";
        sender.nickname        = "张三" + QString::number(i);
        sender.avatar          = QIcon(":/resource/image/defaultAvatar.png");
        sender.description     = "";
        sender.phone           = "18612345678";
        model::Message message = model::Message::makeMessage(model::TEXT_TYPE, "", sender,
                                                             QString("消息内容" + QString::number(i)).toUtf8(), "");
        this->addHistoryMessage(message);
    }
#endif
}

void HistoryMessageWidget::initScrollArea(QGridLayout *layout)
{
    // 1. 创建滚动区域
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; }");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");

    // 2. 创建 QWidget 作为要添加内容的容器
    _container = new QWidget();
    scrollArea->setWidget(_container);

    // 3. 创建 container 中的布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setAlignment(Qt::AlignTop);
    _container->setLayout(vlayout);

    // 4. 把滚动区加入到整个 layout 中
    layout->addWidget(scrollArea, 2, 0, 1, 9);
}

void HistoryMessageWidget::addHistoryMessage(const model::Message &message)
{
    HistoryItem *item = HistoryItem::makeHistoryItem(message);
    _container->layout()->addWidget(item);
}

void HistoryMessageWidget::clear()
{
    QVBoxLayout *layout = dynamic_cast<QVBoxLayout *>(_container->layout());
    for (int i = layout->count() - 1; i >= 0; i--)
    {
        QWidget *w = layout->itemAt(i)->widget();
        if (w == nullptr)
        {
            continue;
        }
        layout->removeWidget(w);
        w->deleteLater();
    }
}
